import sys
import win32api
import win32con
import win32gui
from threading import Thread


class SysTray:
    def __init__(self):
        self.icon_path = "icon.ico"
        self.menu_options = (
            ("显示控制台", None, self.show_console),
            ("暂停服务", None, self.pause_service),
            ("退出", None, self.exit)

    def create(self):
        Thread(target=self._run_tray).start()

    def _run_tray(self):
        message_map = {
            win32con.WM_DESTROY: self.exit,
            win32con.WM_COMMAND: self.command,
        }

        hinst = win32api.GetModuleHandle(None)
        hicon = win32gui.LoadImage(
            hinst, self.icon_path,
            win32con.IMAGE_ICON, 0, 0,
            win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
        )

        hwnd = win32gui.CreateWindow(...)
        win32gui.Shell_NotifyIcon(win32gui.NIM_ADD, (hwnd, hicon))
        win32gui.PumpMessages()